package com.example.ymm.modules1.service.impl.user;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.ymm.base.BaseServiceImpl;
import com.example.ymm.model.TableList;
import com.example.ymm.model.YmPageDto;
import com.example.ymm.modules1.entity.my.user.DbUserSign;
import com.example.ymm.modules1.mapper.my.user.DbUserSignMapper;
import com.example.ymm.modules1.query.DbUserSignQueryCriteria;
import com.example.ymm.modules1.service.user.DbUserSignService;
import com.example.ymm.util.QueryHelpPlus;
import com.example.ymm.util.UtilValidate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
@Slf4j
public class DbUserSignServiceImpl extends BaseServiceImpl<DbUserSignMapper, DbUserSign> implements DbUserSignService {

    @Override
    public TableList<DbUserSign> list(DbUserSignQueryCriteria criteria, YmPageDto pageDto) {
        IPage<DbUserSign> page = new Page<DbUserSign>(pageDto.getPage(),pageDto.getPageSize());
        QueryWrapper predicate = QueryHelpPlus.getPredicate(DbUserSign.class, criteria);
//        predicate.orderByDesc("create_time");
        baseMapper.selectPage(page, predicate);
        TableList<DbUserSign> tableList=new TableList(page);
        return tableList;
    }

    public List<DbUserSign> queryAll(DbUserSignQueryCriteria criteria) {
        List<DbUserSign> result =baseMapper.selectList(QueryHelpPlus.getPredicate(DbUserSign.class, criteria));
        return result;
    }


    @Override
    public void addStartUser(String userId,String userName,String day,String source ) {
        DbUserSign sign=new DbUserSign();
        sign.setUserId(userId);
        sign.setUserName(userName);
        sign.setDay(day);
        DbUserSignQueryCriteria criteria=new DbUserSignQueryCriteria();
        criteria.setUserId(sign.getUserId());
        criteria.setDay(sign.getDay());
        List<DbUserSign> dbUserSigns = queryAll(criteria);
        if(UtilValidate.isEmpty(dbUserSigns)){
            sign.setRemark("起始签到");
            sign.setNum(1);
            sign.setSource(source);
            saveOrUpdate(sign);
        }
    }
}
